/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.general.building;


import gov.fema.hazus.bdc.client.common.ui.AbstractForm;
import gov.fema.hazus.bdc.client.common.ui.BuildingUpdate;
import gov.fema.hazus.bdc.client.common.ui.TextBoxIntegerValueBlurHandler;
import gov.fema.hazus.bdc.client.common.ui.TextBoxLongValueBlurHandler;
import gov.fema.hazus.bdc.client.general.GeneralConstants;
import gov.fema.hazus.bdc.shared.entity.Building;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

public class GeneralBuildingForm extends AbstractForm implements BuildingUpdate {

	// labels
	private Label occupancy;
	private Label buildingType;
	private Label area;
	private Label constructionYear;
	private Label stories;
	
	// form fields
	private OccupancyClassListBox formOccupancy = new OccupancyClassListBox();
	private BuildingStructuralTypeListBox formBuildingType = new BuildingStructuralTypeListBox();
	private TextBox formArea = new TextBox();
	private TextBox formConstructionYear = new TextBox();
	private TextBox formStories = new TextBox();
	
	// ui components
	private DockPanel docker;
	private Grid table = new Grid(5,2);
	
	// listeners
	private GeneralBuildingListBoxChangeListener generalBuildingListBoxChangeListener;
	private GeneralBuildingTextBoxKeyListener generalBuildingTextBoxKeyListener;
	private GeneralConstants constants;
	private TextBoxLongValueBlurHandler textBoxLongValueBlurHandler;
	private TextBoxIntegerValueBlurHandler textBoxIntegerValueBlurHandler;
	
	public GeneralBuildingForm(Building building)
	{
		// do parent
		super(building);
		
		// set up widgets
		docker = new DockPanel();
		
		// set up tab title
		constants = (GeneralConstants)GWT.create(GeneralConstants.class);
		tabTitle = constants.generalBuildingTabTitle();
		
		occupancy = new Label(constants.generalBuildingOccupancy());
		buildingType = new Label(constants.generalBuildingBuildingType());
		area = new Label(constants.generalBuildingArea());
		constructionYear = new Label(constants.generalBuildingConstructionYear());
		stories = new Label(constants.generalBuildingStories());
		
		// add labels to form
		table.setWidget(0, 0, occupancy);
		table.setWidget(1, 0, buildingType);
		table.setWidget(2, 0, area);
		table.setWidget(3, 0, constructionYear);
		table.setWidget(4, 0, stories);
		
		// set up listener
		generalBuildingListBoxChangeListener = new GeneralBuildingListBoxChangeListener(building);
		generalBuildingTextBoxKeyListener = new GeneralBuildingTextBoxKeyListener(building);
		textBoxLongValueBlurHandler = new TextBoxLongValueBlurHandler();
		textBoxIntegerValueBlurHandler = new TextBoxIntegerValueBlurHandler();
		
		// load listeners
		loadListeners();
		
		// load forms
		loadForms();
				
		// add fields to table
		table.setWidget(0, 1, formOccupancy);
		table.setWidget(1, 1, formBuildingType);
		table.setWidget(2, 1, formArea);
		table.setWidget(3, 1, formConstructionYear);
		table.setWidget(4, 1, formStories);
		
		// add table to docker
		docker.add(table, DockPanel.CENTER);
		
		initWidget(docker);
		
	}
	
	private void loadForms()
	{

		if( building != null)
		{
			// set up form fields
			formOccupancy.setListItem(building.getOccupancy());
			formOccupancy.setName( Building.OCCUPANCY );
			
			formBuildingType.setListItem( building.getBuildingType() );
			formBuildingType.setName( Building.BUILDINGTYPE );
			
			String area = null;
			if(building.getArea() != null )
			{
				area = building.getArea().toString();
			}
			formArea.setText( area );
			formArea.setName( Building.AREA );
			
			String constructionYear = null;
			if( building.getConstructionYear() != null )
			{
				constructionYear = building.getConstructionYear().toString();
			}
			formConstructionYear.setText( constructionYear );
			formConstructionYear.setName( Building.CONSTRUCTIONYEAR );
			
			String stories = null;
			if( building.getStories() != null)
			{
				stories = building.getStories().toString();
			}
			formStories.setText( stories );
			formStories.setName( Building.STORIES );
			
	        if( building.getMode().equalsIgnoreCase(Building.READ_ONLY))
	        {
	            formOccupancy.setEnabled(false);
	            formBuildingType.setEnabled(false);
	            formArea.setEnabled(false);
	            formConstructionYear.setEnabled(false);
	            formStories.setEnabled(false);
	        } else
	        {
	            formOccupancy.setEnabled(true);
	            formBuildingType.setEnabled(true);
	            formArea.setEnabled(true);
	            formConstructionYear.setEnabled(true);
	            formStories.setEnabled(true);
	        }
		}
	}
	
	private void loadListeners()
	{
		formOccupancy.addChangeListener(generalBuildingListBoxChangeListener);
		formBuildingType.addChangeListener( generalBuildingListBoxChangeListener );
		
		formArea.addFocusListener(generalBuildingTextBoxKeyListener);
		formArea.addBlurHandler(textBoxLongValueBlurHandler);
		
		formConstructionYear.addFocusListener(generalBuildingTextBoxKeyListener);
		formConstructionYear.addBlurHandler(textBoxIntegerValueBlurHandler);
		
		formStories.addFocusListener( generalBuildingTextBoxKeyListener );
		formStories.addBlurHandler( textBoxIntegerValueBlurHandler );
	}

	public void updateBuilding(Building building) {
		
		// entities
		this.building = building;
		GWT.log("GENERAL BUILDING FORM, EDIT MODE=" + building.getMode(), null);
		
		// listeners
		generalBuildingListBoxChangeListener.updateBuilding(building);
		//generalBuildingListBoxChangeListener.updateBuilding(building);
		//generalBuildingTextBoxKeyListener.updateBuilding(building);
		//generalBuildingTextBoxKeyListener.updateBuilding(building);
		generalBuildingTextBoxKeyListener.updateBuilding(building);
		
		// load forms again (but not listeners)
		loadForms();
		
	}	
	
}
